//
//  HMToolBarStackView.swift
//  WeiBo
//
//  Created by Feng on 2017/6/13.
//  Copyright © 2017年 feng. All rights reserved.
//

import UIKit

//MARK: --设置按钮的枚举值
enum HMComposeToolBarButtonType: Int
{
    //照片
    case picture = 0
    //@
    case mention = 1
    //#
    case trend = 2
    //表情
    case emoticon = 3
    //加号
    case add = 4
}

class HMToolBarStackView: UIStackView {

    //MARK: --设置回调的闭包
    var toolBarCallBack:((HMComposeToolBarButtonType) -> ())?
    
    //表情按钮
    var emoticonButton: UIButton!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK: --设置界面
    private func setupUI()
    {
        //水平布局
        self.axis = .horizontal
        //设置子视图填充方式
        self.distribution = .fillEqually
        //间距
//        self.spacing
        addChildButton(imageName: "compose_toolbar_picture", buttonType: .picture)
        addChildButton(imageName: "compose_mentionbutton_background", buttonType: .mention)
        addChildButton(imageName: "compose_trendbutton_background", buttonType: .trend)
        emoticonButton = addChildButton(imageName: "compose_emoticonbutton_background", buttonType: .emoticon)
        addChildButton(imageName: "compose_add_background", buttonType: .add)
    }
    
    //可以忽略返回值
    @discardableResult
    private func addChildButton(imageName: String,buttonType: HMComposeToolBarButtonType) -> UIButton
    {
        let button = UIButton()
        button.setImage(UIImage(named:imageName), for: .normal)
        button.setImage(UIImage(named:"\(imageName)_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named:"compose_toolbar_background"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        button.tag = buttonType.rawValue
        
        //按钮注册点击事件
        button.addTarget(self, action: #selector(toolBarButtonAction(btn:)), for: .touchUpInside)
        
        addArrangedSubview(button)
        
        return button
    }
    
    @objc func toolBarButtonAction(btn: UIButton)
    {
        toolBarCallBack?(HMComposeToolBarButtonType(rawValue: btn.tag)!)
    }
    
    //根据键盘类型切换icon
    func switchButtonIcon(isEmoticonButton:Bool)
    {
        if isEmoticonButton
        {
            //  表情键盘对应的系统键盘的icon
            emoticonButton.setImage(UIImage(named: "compose_keyboardbutton_background"), for: .normal)
            emoticonButton.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), for: .highlighted)
        }
        else
        {
            //  系统键盘对应的表情键盘的icon
            emoticonButton.setImage(UIImage(named: "compose_emoticonbutton_background"), for: .normal)
            emoticonButton.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), for: .highlighted)
        }
    }
}
